package http

import (
	"fmt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"telemple_web/app"
	"telemple_web/app/http/routers"
	"telemple_web/app/http/validation"
)

func NerServer() error {
	r := gin.Default()
	//r := gin.New()
	//if app.Config.WebServerLog.Enabled {
	//	r.Use(logger.New(app.Config.WebServerLog))
	//}
	//r.Use(gin.Recovery())

	///自定义表单验证器
	err := validation.RegisterValidation()
	if err != nil {
		panic(fmt.Sprintf("register validation error :%s", err.Error()))
	}
	///全局中间件
	//>>>>>>>>>>>>>>>>>>>>>>>全局跨域配置开始<<<<<<<<<<<<<<<<<<<<<<<<
	// CORS for https://foo.com and https://github.com origins, allowing:
	// - PUT and PATCH methods
	// - Origin header
	// - Credentials share
	// - Preflight requests cached for 12 hours
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://127.0.0.1"},
		AllowMethods:     []string{"PUT", "PATCH", "GET", "POST"},
		AllowHeaders:     []string{"Origin", "Content-Length", "Content-Type"},
		ExposeHeaders:    []string{"Content-Length", "Accept-Encoding"},
		AllowCredentials: true,
		//AllowOriginFunc: func(origin string) bool {
		//	return origin == "https://github.com"
		//},
		////控制缓存过期时间的一个指令
		//MaxAge: 12 * time.Hour,
	}))
	//>>>>>>>>>>>>>>>>>>>>>>>全局跨域配置结束<<<<<<<<<<<<<<<<<<<<<<<<

	routers.Register(r)
	return r.Run(fmt.Sprintf(":%s", app.Config.Http.Listen))
}
